/**
 * 音频管理器
 * @description 管理游戏中的所有音效和背景音乐
 */
export class AudioManager {
    constructor() {
        this.sounds = {};
        this.isMuted = false;
        this.volume = 0.5;

        this.initSounds();
    }

    /**
     * 初始化音效
     */
    initSounds() {
        try {
            // 创建音效上下文
            this.audioContext = new (window.AudioContext || window.webkitAudioContext)();

            // 初始化各种音效
            this.createPaddleHitSound();
            this.createWallHitSound();
            this.createScoreSound();
            this.createStartSound();
            this.createPauseSound();
            this.createResumeSound();
            this.createGameOverSound();
            this.createResetSound();
        } catch (error) {
            console.warn('音频初始化失败:', error);
            this.audioContext = null;
        }
    }

    /**
     * 创建球拍碰撞音效
     */
    createPaddleHitSound() {
        const oscillator = this.audioContext.createOscillator();
        const gainNode = this.audioContext.createGain();

        oscillator.connect(gainNode);
        gainNode.connect(this.audioContext.destination);

        oscillator.frequency.setValueAtTime(800, this.audioContext.currentTime);
        oscillator.frequency.exponentialRampToValueAtTime(400, this.audioContext.currentTime + 0.1);

        gainNode.gain.setValueAtTime(this.volume, this.audioContext.currentTime);
        gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.1);

        this.sounds.paddleHit = { oscillator, gainNode };
    }

    /**
     * 创建墙壁碰撞音效
     */
    createWallHitSound() {
        const oscillator = this.audioContext.createOscillator();
        const gainNode = this.audioContext.createGain();

        oscillator.connect(gainNode);
        gainNode.connect(this.audioContext.destination);

        oscillator.frequency.setValueAtTime(600, this.audioContext.currentTime);
        oscillator.frequency.exponentialRampToValueAtTime(300, this.audioContext.currentTime + 0.05);

        gainNode.gain.setValueAtTime(this.volume * 0.7, this.audioContext.currentTime);
        gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.05);

        this.sounds.wallHit = { oscillator, gainNode };
    }

    /**
     * 创建得分音效
     */
    createScoreSound() {
        const oscillator = this.audioContext.createOscillator();
        const gainNode = this.audioContext.createGain();

        oscillator.connect(gainNode);
        gainNode.connect(this.audioContext.destination);

        oscillator.frequency.setValueAtTime(400, this.audioContext.currentTime);
        oscillator.frequency.exponentialRampToValueAtTime(800, this.audioContext.currentTime + 0.2);
        oscillator.frequency.exponentialRampToValueAtTime(400, this.audioContext.currentTime + 0.4);

        gainNode.gain.setValueAtTime(this.volume, this.audioContext.currentTime);
        gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.4);

        this.sounds.score = { oscillator, gainNode };
    }

    /**
     * 创建开始游戏音效
     */
    createStartSound() {
        const oscillator = this.audioContext.createOscillator();
        const gainNode = this.audioContext.createGain();

        oscillator.connect(gainNode);
        gainNode.connect(this.audioContext.destination);

        oscillator.frequency.setValueAtTime(300, this.audioContext.currentTime);
        oscillator.frequency.exponentialRampToValueAtTime(600, this.audioContext.currentTime + 0.1);
        oscillator.frequency.exponentialRampToValueAtTime(900, this.audioContext.currentTime + 0.2);

        gainNode.gain.setValueAtTime(this.volume, this.audioContext.currentTime);
        gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.2);

        this.sounds.start = { oscillator, gainNode };
    }

    /**
     * 创建暂停音效
     */
    createPauseSound() {
        const oscillator = this.audioContext.createOscillator();
        const gainNode = this.audioContext.createGain();

        oscillator.connect(gainNode);
        gainNode.connect(this.audioContext.destination);

        oscillator.frequency.setValueAtTime(500, this.audioContext.currentTime);
        oscillator.frequency.exponentialRampToValueAtTime(300, this.audioContext.currentTime + 0.1);

        gainNode.gain.setValueAtTime(this.volume * 0.8, this.audioContext.currentTime);
        gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.1);

        this.sounds.pause = { oscillator, gainNode };
    }

    /**
     * 创建恢复音效
     */
    createResumeSound() {
        const oscillator = this.audioContext.createOscillator();
        const gainNode = this.audioContext.createGain();

        oscillator.connect(gainNode);
        gainNode.connect(this.audioContext.destination);

        oscillator.frequency.setValueAtTime(300, this.audioContext.currentTime);
        oscillator.frequency.exponentialRampToValueAtTime(600, this.audioContext.currentTime + 0.1);

        gainNode.gain.setValueAtTime(this.volume * 0.8, this.audioContext.currentTime);
        gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.1);

        this.sounds.resume = { oscillator, gainNode };
    }

    /**
     * 创建游戏结束音效
     */
    createGameOverSound() {
        const oscillator = this.audioContext.createOscillator();
        const gainNode = this.audioContext.createGain();

        oscillator.connect(gainNode);
        gainNode.connect(this.audioContext.destination);

        oscillator.frequency.setValueAtTime(800, this.audioContext.currentTime);
        oscillator.frequency.exponentialRampToValueAtTime(400, this.audioContext.currentTime + 0.2);
        oscillator.frequency.exponentialRampToValueAtTime(200, this.audioContext.currentTime + 0.4);

        gainNode.gain.setValueAtTime(this.volume, this.audioContext.currentTime);
        gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.4);

        this.sounds.gameOver = { oscillator, gainNode };
    }

    /**
     * 创建重置音效
     */
    createResetSound() {
        const oscillator = this.audioContext.createOscillator();
        const gainNode = this.audioContext.createGain();

        oscillator.connect(gainNode);
        gainNode.connect(this.audioContext.destination);

        oscillator.frequency.setValueAtTime(400, this.audioContext.currentTime);
        oscillator.frequency.exponentialRampToValueAtTime(200, this.audioContext.currentTime + 0.1);
        oscillator.frequency.exponentialRampToValueAtTime(600, this.audioContext.currentTime + 0.2);

        gainNode.gain.setValueAtTime(this.volume * 0.9, this.audioContext.currentTime);
        gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.2);

        this.sounds.reset = { oscillator, gainNode };
    }

    /**
 * 播放音效
 */
    playSound(soundName) {
        if (this.isMuted || !this.sounds[soundName] || !this.audioContext) return;

        try {
            // 重新创建音效以避免重复播放问题
            this.createSound(soundName);

            const sound = this.sounds[soundName];
            if (sound && sound.oscillator && sound.gainNode) {
                sound.oscillator.start();
                sound.oscillator.stop(this.audioContext.currentTime + 0.2);
            }
        } catch (error) {
            console.warn('音频播放失败:', error);
        }
    }

    /**
     * 创建指定音效
     */
    createSound(soundName) {
        switch (soundName) {
            case 'paddleHit':
                this.createPaddleHitSound();
                break;
            case 'wallHit':
                this.createWallHitSound();
                break;
            case 'score':
                this.createScoreSound();
                break;
            case 'start':
                this.createStartSound();
                break;
            case 'pause':
                this.createPauseSound();
                break;
            case 'resume':
                this.createResumeSound();
                break;
            case 'gameOver':
                this.createGameOverSound();
                break;
            case 'reset':
                this.createResetSound();
                break;
        }
    }

    /**
     * 设置音量
     */
    setVolume(volume) {
        this.volume = Math.max(0, Math.min(1, volume));
    }

    /**
     * 静音/取消静音
     */
    toggleMute() {
        this.isMuted = !this.isMuted;
        return this.isMuted;
    }

    /**
     * 设置静音状态
     */
    setMuted(muted) {
        this.isMuted = muted;
    }

    /**
     * 获取音量
     */
    getVolume() {
        return this.volume;
    }

    /**
     * 获取静音状态
     */
    isMuted() {
        return this.isMuted;
    }
} 